package com.example.demo.sensitive.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.sensitive.dto.request.DatabaseConfigRequest;
import com.example.demo.sensitive.dto.response.DatabaseConfigResponse;
import com.example.demo.sensitive.service.DatabaseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;

/**
 * @ClassName DatabaseController
 * @Description 数据库管理控制器
 * @Author System
 * @Date 2025/9/19
 **/
@RestController
@RequestMapping("/api/databases")
@Tag(name = "数据库管理", description = "数据库配置相关操作接口")
public class DatabaseController {

    @Resource
    private DatabaseService databaseService;

    @GetMapping
    @Operation(summary = "分页查询数据库配置", description = "根据条件分页查询数据库配置列表")
    public Result<Page<DatabaseConfigResponse>> getPageList(
            @Parameter(description = "当前页码") @RequestParam(defaultValue = "1") int current,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "20") int size,
            @Parameter(description = "状态") @RequestParam(required = false) Integer status,
            @Parameter(description = "数据库类型") @RequestParam(required = false) String dbType,
            @Parameter(description = "关键词") @RequestParam(required = false) String keyword) {
        
        Page<DatabaseConfigResponse> result = databaseService.getPageList(current, size, status, dbType, keyword);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询数据库配置详情", description = "根据ID查询数据库配置详情")
    public Result<DatabaseConfigResponse> getById(
            @Parameter(description = "配置ID") @PathVariable Long id) {
        
        DatabaseConfigResponse result = databaseService.getDatabaseConfigById(id);
        return Result.success(result);
    }

    @PostMapping
    @Operation(summary = "创建数据库配置", description = "添加新的数据库配置")
    public Result<Long> create(@Valid @RequestBody DatabaseConfigRequest request) {
        Long id = databaseService.create(request);
        return Result.success("创建成功", id);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新数据库配置", description = "更新指定数据库配置信息")
    public Result<String> update(
            @Parameter(description = "配置ID") @PathVariable Long id,
            @Valid @RequestBody DatabaseConfigRequest request) {
        
        databaseService.update(id, request);
        return Result.success("更新成功", "success");
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除数据库配置", description = "删除指定数据库配置")
    public Result<String> delete(
            @Parameter(description = "配置ID") @PathVariable Long id) {
        
        databaseService.delete(id);
        return Result.success("删除成功", "success");
    }

    @PutMapping("/{id}/status")
    @Operation(summary = "更新配置状态", description = "启用或禁用数据库配置")
    public Result<String> updateStatus(
            @Parameter(description = "配置ID") @PathVariable Long id,
            @Parameter(description = "状态") @RequestParam Integer status) {
        
        databaseService.updateStatus(id, status);
        return Result.success("状态更新成功", "success");
    }

    @GetMapping("/enabled")
    @Operation(summary = "查询启用的配置", description = "查询所有启用状态的数据库配置")
    public Result<List<DatabaseConfigResponse>> getEnabledConfigs() {
        List<DatabaseConfigResponse> result = databaseService.getEnabledConfigs();
        return Result.success(result);
    }

    @PostMapping("/{id}/test-connection")
    @Operation(summary = "测试数据库连接", description = "测试指定数据库配置的连接")
    public Result<DatabaseService.ConnectionTestResult> testConnection(
            @Parameter(description = "配置ID") @PathVariable Long id) {
        
        DatabaseService.ConnectionTestResult result = databaseService.testConnection(id);
        return Result.success(result);
    }

    @PostMapping("/test-connection")
    @Operation(summary = "测试连接配置", description = "测试数据库连接配置（无需保存）")
    public Result<DatabaseService.ConnectionTestResult> testConnection(
            @Valid @RequestBody DatabaseConfigRequest request) {
        
        DatabaseService.ConnectionTestResult result = databaseService.testConnection(request);
        return Result.success(result);
    }

    @GetMapping("/{id}/tables")
    @Operation(summary = "获取数据库表列表", description = "获取指定数据库的所有表")
    public Result<List<DatabaseService.TableInfo>> getTables(
            @Parameter(description = "数据库ID") @PathVariable Long id) {
        
        List<DatabaseService.TableInfo> result = databaseService.getTables(id);
        return Result.success(result);
    }

    @GetMapping("/{id}/tables/{tableName}/structure")
    @Operation(summary = "获取表结构", description = "获取指定表的结构信息")
    public Result<DatabaseService.TableStructure> getTableStructure(
            @Parameter(description = "数据库ID") @PathVariable Long id,
            @Parameter(description = "表名") @PathVariable String tableName) {
        
        DatabaseService.TableStructure result = databaseService.getTableStructure(id, tableName);
        return Result.success(result);
    }

    @GetMapping("/statistics")
    @Operation(summary = "数据库类型统计", description = "统计各类型数据库的数量")
    public Result<List<DatabaseService.DbTypeStatistics>> getDbTypeStatistics() {
        List<DatabaseService.DbTypeStatistics> result = databaseService.getDbTypeStatistics();
        return Result.success(result);
    }
}
